﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IGGWindowApp.Data;
using IGGCore;

namespace IGGWindowApp.Panels
{
    public partial class PanelStadium : UserControl
    {
        // EVENTS
        public delegate void ItemSelectedHandler(string item);
        public event ItemSelectedHandler ItemSelected;

        private Label[] lblStadiumPlayers;

        public PanelStadium()
        {
            InitializeComponent();
            lblStadiumPlayers = new Label[13];
            for (int i = 0; i < 13; i++)
            {
                lblStadiumPlayers[i] = new Label();
                lblStadiumPlayers[i].BackColor = Color.White;
                lblStadiumPlayers[i].BorderStyle = BorderStyle.FixedSingle;
                lblStadiumPlayers[i].Font = new Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblStadiumPlayers[i].Location = new System.Drawing.Point(324, 216);
                lblStadiumPlayers[i].Name = "lblPlayer" + i;
                lblStadiumPlayers[i].Size = new System.Drawing.Size(80, 17);
                lblStadiumPlayers[i].TabIndex = 14;
                lblStadiumPlayers[i].Text = "J. Player";
                lblStadiumPlayers[i].TextAlign = ContentAlignment.MiddleCenter;
                lblStadiumPlayers[i].Visible = false;
                panelImage.Controls.Add(lblStadiumPlayers[i]);
            }
        }

        private void panelTitle_ItemSelected(string item)
        {
            ItemSelected(item);
        }

        public void SetData(WindowStadium stadium, Game selectedGame)
        {
            //panelTitle.UpdateItem(stadium.Name, 5);

            int hour = DateTime.Now.Hour;
            hour = hour - selectedGame.HomeTeam.TeamID.TimeZoneDiff;

            if (selectedGame.Status.Indicator.Category == Indicator.IndicatorCategoy.INTERRUPTED)
            {
                panelImage.BackgroundImage = stadium.StadiumImageRain;
            }
            else if (selectedGame.Status.Indicator.Category == Indicator.IndicatorCategoy.IN_PROGRESS)
            {
                int[][] pitchingColors = new int[2][];
                int[][] battingColors = new int[2][];
                Team pitchingTeam = selectedGame.HomeTeam;
                Team battingTeam = selectedGame.AwayTeam;

                if (selectedGame.Status.InningTop == false)
                {
                    pitchingTeam = selectedGame.AwayTeam;
                    pitchingColors[0] = pitchingTeam.TeamID.BackColorAway;
                    pitchingColors[1] = pitchingTeam.TeamID.ForeColorAway;
                    battingTeam = selectedGame.HomeTeam;
                    battingColors[0] = battingTeam.TeamID.BackColor;
                    battingColors[1] = battingTeam.TeamID.ForeColor;
                }
                else
                {
                    pitchingColors[0] = pitchingTeam.TeamID.BackColor;
                    pitchingColors[1] = pitchingTeam.TeamID.ForeColor;
                    battingColors[0] = battingTeam.TeamID.BackColorAway;
                    battingColors[1] = battingTeam.TeamID.ForeColorAway;
                }

                Color pitchingColor = Color.FromArgb(pitchingColors[0][0],
                    pitchingColors[0][1], pitchingColors[0][2]);
                Color pitchingForeColor = Color.FromArgb(pitchingColors[1][0],
                    pitchingColors[1][1], pitchingColors[1][2]);

                Color battingColor = Color.FromArgb(battingColors[0][0],
                    battingColors[0][1], battingColors[0][2]);
                Color battingForeColor = Color.FromArgb(battingColors[1][0],
                    battingColors[1][1], battingColors[1][2]);
                for (int i = 0; i < 13; i++)
                {
                    if (i < 9)
                    {
                        lblStadiumPlayers[i].BackColor = pitchingColor;
                        lblStadiumPlayers[i].ForeColor = pitchingForeColor;
                    }
                    else
                    {
                        lblStadiumPlayers[i].BackColor = battingColor;
                        lblStadiumPlayers[i].ForeColor = battingForeColor;
                    }
                }
                if (hour < 18)
                {
                    panelImage.BackgroundImage = stadium.StadiumImageDay;
                    for (int i = 0; i < 13; i++)
                    {
                        lblStadiumPlayers[i].Location = stadium.StadiumDayPositions[i];
                        lblStadiumPlayers[i].Visible = true;
                    }
                }
                else if (hour < 19)
                {
                    panelImage.BackgroundImage = stadium.StadiumImageDusk;
                    for (int i = 0; i < 13; i++)
                    {
                        lblStadiumPlayers[i].Location = stadium.StadiumDuskPositions[i];
                        lblStadiumPlayers[i].Visible = true;
                    }
                }
                else
                {
                    panelImage.BackgroundImage = stadium.StadiumImageNight;
                    for (int i = 0; i < 13; i++)
                    {
                        lblStadiumPlayers[i].Location = stadium.StadiumNightPositions[i];
                        lblStadiumPlayers[i].Visible = true;
                    }
                }
                SetPlayer(selectedGame.Status.CurrentBatter, lblStadiumPlayers[CoreConstants.POSITION_BATTER]);
                for (int i = 0; i < 9; i++)
                {
                    SetPlayer(pitchingTeam.Defense[i], lblStadiumPlayers[i]);
                    lblStadiumPlayers[i].Visible = true;
                    lblStadiumPlayers[i].Refresh();
                }
                SetRunner(selectedGame.Status.RunnersOnBase[0], lblStadiumPlayers[10]);
                SetRunner(selectedGame.Status.RunnersOnBase[1], lblStadiumPlayers[11]);
                SetRunner(selectedGame.Status.RunnersOnBase[2], lblStadiumPlayers[12]);
            }
            else
            {
                panelImage.BackgroundImage = stadium.StadiumImageEmpty;
                for (int i = 0; i < 13; i++)
                {
                    lblStadiumPlayers[i].Visible = false;
                }
            }
            panelImage.Refresh();




        }

        /// <summary>
        /// Set a player text and visibility
        /// </summary>
        private void SetPlayer(Player player, Label label)
        {
            if (player != null)
            {
                label.Text = player.PartialName;
                label.Visible = true;
            }
            else
            {
                label.Visible = false;
            }
        }

        /// <summary>
        /// Set the runner text and visibility
        /// </summary>
        private void SetRunner(Player runner, Label label)
        {
            if (runner != null)
            {
                label.Visible = true;
                label.Text = runner.PartialName;
            }
            else
            {
                label.Visible = false;
            }
        }
    }
}
